﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

namespace GMS.BIZ
{
    public class Classes
    {
        #region Property
        private string classTeacher;
        public string ClassTeacher
        {
            get { return classTeacher; }
            set { classTeacher = value; }
        }
        #endregion Property

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="classTeacher">班主任工号</param>
        public Classes(string classTeacher)
        {
            this.classTeacher = classTeacher;
        }

        /// <summary>
        /// 班主任的所带班级
        /// </summary>
        public int Count()
        {
            //UClass中班主任为本教师的班级数量
            GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
            DataSet ds = optUClass.GetList("class_teacher='" + this.classTeacher + "' ORDER BY class_id");
            return ds.Tables[0].Rows.Count;       
        }

        /// <summary>
        /// 为班主任添加一个所带班级
        /// </summary>
        /// <param name="classID">班级ID</param>
        public string Add(string classID)
        {
            //班级是否存在，若不存在
            GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
            if(!optUClass.Exists(classID))
            return ErrorMessage.NoSuchData;

            //本班级是否已有班主任，若已经有
            if(optUClass.GetModel(classID).class_teacher!=string.Empty)
            return ErrorMessage.AlreadyExitsData;

            //添加
            GMS.ORM.Model.UClass myUClass=new GMS.ORM.Model.UClass();
            myUClass.class_id=classID;
            myUClass.class_teacher = this.ClassTeacher;
            bool ok = optUClass.Update(myUClass);
            if (ok) return ErrorMessage.OK;
            else return ErrorMessage.DBOptError; 
        }

        /// <summary>
        /// 去掉一个所带班级
        /// </summary>
        /// <param name="classID">班级ID</param>
        public string Delete(string classID)
        {
            //班级是否存在，若不存在
            GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
            if (!optUClass.Exists(classID))
                return ErrorMessage.NoSuchData;

            //本班级的班主任是否为本教师，若不是
            if (!((optUClass.GetModel(classID).class_teacher).CompareTo(this.classTeacher)==0))
            return ErrorMessage.DataNotMatch;

            //删除本条记录的班主任信息，班主任信息置空
            GMS.ORM.Model.UClass myUC = new GMS.ORM.Model.UClass();
            myUC.class_id = classID;
            myUC.class_teacher = null;
            bool ok = optUClass.Update(myUC);
            if (!ok) return ErrorMessage.DBOptError;
            return ErrorMessage.OK;            
        }

        /// <summary>
        /// 更新一个班级的班主任
        /// </summary>
        /// <param name="classID">班级ID</param>
        public string Update(string classID)
        {
            /* 可以直接调用本类中的Delete与Add
             * 先将这个班级的班主任Delete掉
             * 然后再使用Add加上新的班主任信息
             * 
             * Add函数只能添加班主任，而本函数有能力将原有班主任信息擦除
             */
            //班级是否存在，若不存在
            GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
            if (!optUClass.Exists(classID))
                return ErrorMessage.NoSuchData;
            //更新班主任信息
            GMS.ORM.Model.UClass myUC = new GMS.ORM.Model.UClass();
            myUC.class_id = classID;
            myUC.class_teacher = this.classTeacher;
            bool ok = optUClass.Update(myUC);
            if (!ok) return ErrorMessage.DBOptError;
            return ErrorMessage.OK;            
        }

        /// <summary>
        /// 获得本教师所带一个班级的对象
        /// </summary>
        /// <param name="classID">班级ID</param>
        public ClassInfo GetOne(string classID)
         {
            //是否存在这个班级，是否班主任为本人
             GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
             if (!optUClass.Exists(classID))
                 return null;
             if (!((optUClass.GetModel(classID).class_teacher).CompareTo(this.classTeacher) == 0))
                 return null;
            //新建并填充classInfo的信息后返回对象
             GMS.BIZ.ClassInfo myClassInfo = new ClassInfo(classID);
             return myClassInfo;
        }

        /// <summary>
        /// 获得本班主任所带班级的ID列表
        /// </summary>
        public ArrayList GetList()
        {
            GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
            DataSet ds = optUClass.GetList("class_teacher='" + this.classTeacher + "' ORDER BY class_id");
            ArrayList arraylist = new ArrayList();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                arraylist.Add(ds.Tables[0].Rows[i]["class_id"].ToString());
            }
            return arraylist;
        }

        /// <summary>
        /// 获得本班主任所带班级的DataSet
        /// </summary>
        public DataSet GetDataSet()
        {
            //使用DAL.UClass中的GetList
            GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
            DataSet ds = optUClass.GetList("1=1 ORDER BY class_id");
            return ds;
        }
    }
}
